/*
 * @(#)GetDocumentsRunner.java
 * Date 2013-09-06
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.query.es;

import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;

import us.hornerscorners.lamppost.ihecommon.service.ElasticSearchSource;

import us.hornerscorners.lamppost.model.common.PersistableModelType;

import us.hornerscorners.lamppost.registry.constant.QueryField;
import us.hornerscorners.lamppost.registry.constant.QuerySlotName;

import us.hornerscorners.lamppost.registry.model.QueryRequest;

import us.hornerscorners.lamppost.registry.transformer.model.CodedElementTransformer;
import us.hornerscorners.lamppost.registry.transformer.model.PatientIdentifierTransformer;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-06
 * @author         Jim Horner
 */
public class GetDocumentsRunner extends AbstractQueryRunner {

    /**
     * Constructs ...
     *
     *
     * @param config
     * @param elasticSearchSource
     * @param codedElementTransformer
     * @param patientIdentifierTransformer
     */
    public GetDocumentsRunner(
            IHEConfig config, ElasticSearchSource elasticSearchSource,
            CodedElementTransformer codedElementTransformer,
            PatientIdentifierTransformer patientIdentifierTransformer) {

        super(config, elasticSearchSource, codedElementTransformer,
              patientIdentifierTransformer);

    }

    /**
     * Method description
     *
     *
     * @param request
     *
     * @return
     *
     */
    @Override
    public Map<PersistableModelType, QueryBuilder> createQuery(QueryRequest request) {

        Map<PersistableModelType, QueryBuilder> result = new HashMap<>();

        BoolQueryBuilder query = QueryBuilders.boolQuery();

        result.put(PersistableModelType.DocumentEntry, query);

        if (request.hasValues(QuerySlotName.XDSDocumentEntryUniqueId)) {

            query.must(createUniqueIdentifierQuery(request,
                    QuerySlotName.XDSDocumentEntryUniqueId,
                    QueryField.DocumentEntryUniqueIdRoot,
                    QueryField.DocumentEntryUniqueIdExtension));
        }

        if (request.hasValues(QuerySlotName.XDSDocumentEntryEntryUUID)) {

            query.must(createStringQuery(request,
                                         QuerySlotName.XDSDocumentEntryEntryUUID,
                                         QueryField.DocumentEntryEntryUUID));
        }

        return result;
    }
}
